home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6819 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  76 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Confusion as to the proper use of MODULAS
  5. Date: 15 Feb 1996 06:59:41 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4fvhotINN7fv@keats.ugrad.cs.ubc.ca>
  8. References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fthsu$1kl@ixnews7.ix.netcom.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4fthsu$1kl@ixnews7.ix.netcom.com>, KPN  <wzjn@ix.netcom.com> wrote:
  12.  >Confusion as to the proper use of MODULAS
  13.  >
  14.  >Very confused. Just as I think IÆve got it correct, I make-up a new
  15.  >test, the results of which leave me mixed-up again.
  16.  >
  17.  >To begin with, this is the first rule I found out:
  18.  >
  19.  >Modulas operator simply shows:  *** AFTER *** you divide, what is the
  20.  >remainder?
  21.  >EXAMPLE of this:
  22.  >    9 % 7 = ?
  23.  >    9 divided by 7 yields a remainder of 2
  24.  >    9 % 7 = 2
  25.  >
  26.  >1) MODULAS means divide a number and give me the left over number. OK -
  27.  >not bad.
  28.  >
  29.  >Next rule:
  30.  >If the number to be divided is smaller than the number to divide by,
  31.  >the result will always be the number being divided
  32.  >EXAMPLE of this:
  33.  >
  34.  >   9 % 12 = ?
  35.  >   can not divide 9 by a larger number
  36.  >   9 % 12 = 9
  37.  >Now I have rule #2
  38.  >
  39.  >2) If the number to be divided is smaller than the nuber to be used as
  40.  >a divisor, the result is the original number. OK - again, not bad.
  41.  
  42. Umm.... these are not two rules but one! 9 div 12 = 0 , remainder 9.
  43.  
  44. Btw, it's "modulus", not "modulas". 
  45.  
  46.  >Here, begin my troubles: using MODULAS in an IF statement.
  47.  >
  48.  >I made up a test to see if a number I inputted was a 7. Sample code:
  49.  >
  50.  >   if (number % 7)
  51.  >      printf("Not a 7\n");
  52.  >   else
  53.  >      printf("First integer was a 7\n");
  54.  >
  55.  >So, if the number WAS a 7, the ELSE would take over. OK. But, when I
  56.  >enter in a ZERO, it still tells me that the number was a seven?
  57.  
  58. You mean, if the number was "congruent to 0 (modulo 7)". You are not testing
  59. for equality with 7, but for divisibility by 7.
  60.  
  61. The number zero is congruent to 0 (mod 7), so when you put in a zero it tells
  62. you that it is congruent, as is 7, 14, 21, 28, etc.
  63.  
  64. Note that the % operator only gives you the smallest positive residue
  65. (i.e. remainder) if both operands are unsigned quantities. It does not
  66. necessarily give a positive remainder for negative numbers.
  67.  
  68.  >Am I going in the right direction? Can someone tell me what IÆm doing
  69.  >incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
  70.  >in an IF statement?
  71.  
  72. Ummm... ever hear of the == operator? As in,  if (number == 7) ...
  73.  
  74. -- 
  75.  
  76.